﻿@using Storage.Admin.Framework.Menu

@helper RenderMenuItem(DescriperAttribute item, List<DescriperAttribute> list)
{
string href = string.Empty;
if (!String.IsNullOrEmpty(item.RouteName))
{
    href = Url.RouteUrl(item.RouteName);
}
if (!String.IsNullOrEmpty(item.Controller) && !String.IsNullOrEmpty(item.Action))
{
    href = Url.Action(item.Action, item.Controller);
}
    <li id="@item.ResouceID" class="@(Request.Url.LocalPath.Equals(href,StringComparison.InvariantCultureIgnoreCase)?"active":"")">
        @{
            var childNodes = list.Where(o => o.FatherResource == item.SysResource && o.IsMenu).ToList();
        }
        <a href="@(!String.IsNullOrEmpty(href)?"/admin/home#page"+href:"javascript:")" data-url="page@(href)" class="@(childNodes.Any()?"dropdown-toggle":"")">
            <i class="@item.CssClass"></i>
            <span class="menu-text"> @item.Name </span>
            @if (childNodes.Any())
            {
                <b class="arrow fa fa-angle-down"></b>
            }
        </a>

        <b class="arrow"></b>
        @if (childNodes.Any())
        {
            <ul class="submenu">
                @foreach (var childItem in childNodes)
                {
                    if (authorizeSysResource(childItem.SysResource))
                    {
                        @RenderMenuItem(childItem, list)
                    }
                }
            </ul>
        }
    </li>
            if (Request.Url.LocalPath.Equals(href, StringComparison.InvariantCultureIgnoreCase))
            {
                <script>
                    $('#@(item.FatherID)').addClass('open active').parents('li').addClass('open active')
                </script>
            }
}
<div id="sidebar" class="sidebar responsive sidebar-fixed">
    <div class="sidebar-shortcuts" id="sidebar-shortcuts">
        <div class="sidebar-shortcuts-large" id="sidebar-shortcuts-large">
            <button class="btn btn-success">
                <i class="ace-icon fa fa-signal"></i>
            </button>
            <button class="btn btn-info">
                <i class="ace-icon fa fa-pencil"></i>
            </button>
            <button class="btn btn-warning">
                <i class="ace-icon fa fa-users"></i>
            </button>
            <button class="btn btn-danger">
                <i class="ace-icon fa fa-cogs"></i>
            </button>
        </div>
    </div>

    <ul class="nav nav-list">
        @{
            List<DescriperAttribute> list = null;
            if (WorkContext.CurrentUser.IsAdmin)
            {
                list = ReflectManager.Instance.Descripers;
            }
            else
            {
                list = WorkContext.MyDescriperes;
            }

            var nodes = list.Where(o => String.IsNullOrEmpty(o.FatherResource)).Where(o => o.IsMenu).OrderBy(o => o.Sort).ThenBy(o => o.Name).ToList();
            if (nodes != null)
            {
                foreach (var item in nodes)
                {
                    @RenderMenuItem(item, list)
                }
            }

        }
    </ul><!-- /.nav-list -->
    @{
        string controller = Request.RequestContext.RouteData.Values["controller"].ToString();
        string action = Request.RequestContext.RouteData.Values["action"].ToString();
        //当前
        var current = list.FirstOrDefault(o => o.Action == action && o.Controller == controller);
        if (current != null && !current.IsMenu)
        {
            <script>
                 $('#@(current.FatherID)').addClass('active').parents('li').addClass('open active')
            </script>
        }
    }
    <div class="sidebar-toggle sidebar-collapse" id="sidebar-collapse">
        <i class="ace-icon fa fa-angle-double-left" data-icon1="ace-icon fa fa-angle-double-left" data-icon2="ace-icon fa fa-angle-double-right"></i>
    </div>
    <script type="text/javascript">
                 jQuery(function ($) {
                     if ('enable_ajax_content' in ace) {
                         var options = {
                             content_url: function (url) {
                                 //this is for Ace demo only, you should change it
                                 //please refer to documentation for more info

                                 if (!url.match(/^page\//)) return false;

                                 var path = document.location.pathname;
                                 console.log(url);
                                 //for Ace HTML demo version, convert ajax.html#page/gallery to > gallery.html and load it
                                 if (path.match(/admin\/home/)) return path.replace(/admin\/home/, url.replace(/^page\//, ''));

                                 //for Ace PHP demo version convert "page/dashboard" to "?page=dashboard" and load it
                                 return path + "?" + url.replace(/\//, "=");
                             },
                             //default_url: 'page/index'//default url
                         }
                         ace.enable_ajax_content($, options)
                     }

                 });

    </script>
</div>

